package com.baitiaojun.moments.util;

import com.baitiaojun.moments.config.RedissonConfig;
import org.apache.commons.lang3.BooleanUtils;
import org.redisson.api.RBloomFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

/**
 * @类名 BloomFilterUtil
 * @描述 TODO
 * @作者 白条君
 * @创建日期 2023/2/21 14:47
 * @版本 1.0
 */
@Component
public class BloomFilterUtil {

    @Autowired
    private RedissonConfig redisson;

    public Set<Long> filterMomentsUserIds(String keyPrefix, Set<Long> userIds) {
        RBloomFilter<Object> bloomFilter = redisson.bloomFilter();
        Set<Long> userSet = new HashSet<>();
        Iterator<Long> iterator = userIds.iterator();
        while (iterator.hasNext()) {
            Long userId = iterator.next();
            if (BooleanUtils.isTrue(bloomFilter.contains(keyPrefix + userId))) {
                userSet.add(userId);
            }
        }
        return userSet;
    }
}
